package com.lovegu.three;

import java.util.Random;
import java.util.Scanner;

/**
 * <p>
 * 实现随机文本生成
 * </p>
 *
 * @author 老顾
 * @since 2023/4/4
 */
public class RandomTextGenerator {

    private static final int DEFAULT_LENGTH = 10;
    private static final String DEFAULT_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    private final Random random = new Random();
    private final String characters;

    public RandomTextGenerator() {
        this(DEFAULT_LENGTH, DEFAULT_CHARACTERS);
    }

    public RandomTextGenerator(int length) {
        this(length, DEFAULT_CHARACTERS);
    }

    public RandomTextGenerator(String characters) {
        this(DEFAULT_LENGTH, characters);
    }

    public RandomTextGenerator(int length, String characters) {
        this.characters = characters;
    }

    public String generate(int length) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            builder.append(characters.charAt(index));
        }
        return builder.toString();
    }
    public static void main(String[] args) {
        RandomTextGenerator generator = new RandomTextGenerator();
        String text = generator.generate(1000); // 生成1000个字符的随机文本
        System.out.println(text);
    }
}